 /*
  * Mango CMS
  * Copyright 2008, Mango Web, and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package org.mangocms.model.user;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

/**
 * User group
 * 
 * @author <a href="tanordheim@gmail.com">Trond Arve Nordheim</a>
 * @version $Revision: 1.1 $
 */
@Entity
@Table(name = "mango_groups")
public class Group implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Column(name = "id")
	@Id @GeneratedValue
	private Integer id;
	
	@Column(name = "version")
	@Version
	private Integer version;
	
	@Column(name = "title")
	@Length(max = 255)
	@NotNull
	private String title;

	@OneToMany(fetch = FetchType.LAZY)
	@JoinTable(
		name = "mango_groups_roles_assoc",
		joinColumns = { @JoinColumn(name = "group_id") },
		inverseJoinColumns = { @JoinColumn(name = "role_id") }
	)
	private Set<Role> roles = new HashSet<Role>();

	/**
	 * Create a new Group.
	 */
	public Group() {
	}
	
	/**
	 * Create a new Group.
	 * 
	 * @param title
	 */
	public Group(String title) {
		this.title = title;
	}
	
	/**
	 * Add the specified role to the group.
	 * 
	 * @param role
	 */
	public void addRole(Role role) {
		roles.add(role);
	}
	
	/**
	 * Remove the specified role from the group.
	 * 
	 * @param role
	 */
	public void removeRole(Role role) {
		roles.remove(role);
	}

	/**
	 * Get the id.
	 * 
	 * @return the id.
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * Set the id.
	 * 
	 * @param id The id to set.
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Get the version.
	 * 
	 * @return the version.
	 */
	public Integer getVersion() {
		return version;
	}

	/**
	 * Set the version.
	 * 
	 * @param version The version to set.
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * Get the title.
	 * 
	 * @return the title.
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Set the title.
	 * 
	 * @param title The title to set.
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * Get the roles.
	 * 
	 * @return the roles.
	 */
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * Set the roles.
	 * 
	 * @param roles The roles to set.
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

}
